package com.yang.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.junit.Test;
import com.yang.User;
import com.yang.util.DBHelp;
import com.yang.util.MapRow;

public class UserDao {

	DBHelp<User> dh=new DBHelp<User>();
	public User findUser(String name,String password){
		String sql="select * from t_user where name=? and password=?";
		MR mr=new MR();
		return dh.findObj(sql, mr, name,password);
	}
	public List<User> findList(){
		String sql="select * from t_user";
		MR mr=new MR();
		return dh.findList(sql, mr);
	}
	private class MR implements MapRow<User>{

		@Override
		public User RowMap(ResultSet rs) throws SQLException {
			User u=new User();
			u.setId(rs.getInt("id"));
			u.setName(rs.getString("name"));
			u.setPassword(rs.getString("password"));
			u.setPower(rs.getInt("power"));
			return u;
		}
		
	}
	@Test
	public void test(){
		List<User> list=findList();
		for (User user : list) {
			System.out.println(user.getName());
		}
	}
	public User findUser(String name) {
		String sql="select * from t_user where name=?";
		MR mr=new MR();
		return dh.findObj(sql, mr, name);
	}
	public boolean add(String name, String password) {
		String sql="insert t_user(name,password,power)value(?,?,'1')";
		return dh.add(sql, name,password);
	}
	public boolean delUser(int id) {
		String sql=" delete from t_user where id=?";
		return dh.add(sql, id);
	}
	public boolean addmin(int id) {
		String sql="update t_user set power=4 where id=?";
		return dh.add(sql, id);
	}
}
